#include "mylabel.h"

#include "mylabel.h"
#include <QPainter>
#include <QDebug>
#include <QWheelEvent>
#include <QMenu>
#include <QFileDialog>

MyLabel::MyLabel(QWidget *parent):QLabel(parent)
{
    ZoomValue = 1.0;
    XPtInterval = 0;
    YPtInterval = 0;
    update();
}

//QPainter画图
void MyLabel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPixmap pix;
    pix.loadFromData(originalPicture);
    QPainter painter(this);

    // 根据窗口计算应该显示的图片的大小
    int width = qMin(pix.width(), this->width());
    int height = width * 1.0 / (pix.width() * 1.0 / pix.height());
    height = qMin(height, this->height());
    width = height * 1.0 * (pix.width() * 1.0 / pix.height());

    // 平移
    painter.translate(this->width() / 2 + XPtInterval, this->height() / 2 + YPtInterval);

    // 缩放
    painter.scale(ZoomValue, ZoomValue);

    // 绘制图像
    QRect picRect(-width / 2, -height / 2, width, height);
    painter.drawPixmap(picRect, pix);
}

//鼠标滚轮滚动
void MyLabel::wheelEvent(QWheelEvent *event)
{
    if (QApplication::keyboardModifiers() != Qt::ControlModifier) {
        event->ignore();
        return;
    }

    int value = event->delta();
    if (value > 0)  //放大
        OnZoomInImage();
    else            //缩小
        OnZoomOutImage();

    update();
}

//鼠标摁下事件
void MyLabel::mousePressEvent(QMouseEvent *event)
{
    OldPos = event->pos();
    Pressed = true;
}

//鼠标移动事件
void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
    if (!Pressed)
        return QWidget::mouseMoveEvent(event);

    this->setCursor(Qt::SizeAllCursor);
    QPoint pos = event->pos();
    int xPtInterval = pos.x() - OldPos.x();
    int yPtInterval = pos.y() - OldPos.y();

    XPtInterval += xPtInterval;
    YPtInterval += yPtInterval;

    OldPos = pos;
    update();
}

//鼠标释放事件
void MyLabel::mouseReleaseEvent(QMouseEvent */*event*/)
{
    Pressed = false;
    setCursor(Qt::ArrowCursor);
}

//鼠标双击事件
void MyLabel::mouseDoubleClickEvent(QMouseEvent* event)
{
    Q_UNUSED(event)
    if (!originalPicture.isEmpty()) {
        ZoomableImageWidget *imageWidget = new ZoomableImageWidget();
        QPixmap pixmap;
        pixmap.loadFromData(originalPicture);
        imageWidget->setPixmap(pixmap);
        imageWidget->show();
    }
}

//选择打开图片
void MyLabel::OnSelectImage(QByteArray pic)
{
    originalPicture = pic;
    ZoomValue = 1.0;
    XPtInterval = 0;
    YPtInterval = 0;
    update();
}

//图片放大
void MyLabel::OnZoomInImage()
{
    ZoomValue += 0.05;
    update();
}

//图片缩小
void MyLabel::OnZoomOutImage()
{
    ZoomValue -= 0.05;
    if (ZoomValue <= 0)
    {
        ZoomValue = 0.05;
        return;
    }

    update();
}

//图片还原
void MyLabel::OnPresetImage()
{
    ZoomValue = 1.0;
    XPtInterval = 0;
    YPtInterval = 0;
    update();
}

//右键菜单
void MyLabel::contextMenuEvent(QContextMenuEvent *event)
{
    QPoint pos = event->pos();
    pos = this->mapToGlobal(pos);
    QMenu *menu = new QMenu(this);

    QAction *zoomInAction = new QAction(this);
    zoomInAction->setText("放大");
    connect(zoomInAction, &QAction::triggered, this, &MyLabel::OnZoomInImage);
    menu->addAction(zoomInAction);

    QAction *zoomOutAction = new QAction(this);
    zoomOutAction->setText("缩小");
    connect(zoomOutAction, &QAction::triggered, this, &MyLabel::OnZoomOutImage);
    menu->addAction(zoomOutAction);

    QAction *presetAction = new QAction(this);
    presetAction->setText("还原");
    connect(presetAction, &QAction::triggered, this, &MyLabel::OnPresetImage);
    menu->addAction(presetAction);

    menu->exec(pos);
}

